package com.sicaolong.config;

import com.sicaolong.pojo.Configuration;
import com.sicaolong.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author : sicaolong
 * @description :
 * @date : 2020/10/16  12:18 上午
 */
public class XMLMapperBuilder {
	private Configuration configuration;

	public XMLMapperBuilder(Configuration configuration) {
		this.configuration = configuration;
	}

	public void parse(InputStream inputStream) throws DocumentException {
		Document document = new SAXReader().read(inputStream);
		Element rootElement = document.getRootElement();
		String namespace = rootElement.attributeValue("namespace");
		List<Element> list = rootElement.selectNodes("//select");
		for (Element element : list) {
			MappedStatement mappedStatement = new MappedStatement();
			mappedStatement.setId(element.attributeValue(("id")));
			mappedStatement.setParamterType(element.attributeValue(("requestType")));
			mappedStatement.setParamterType(element.attributeValue(("paramterType")));
			mappedStatement.setSql(element.getTextTrim());
			String key = namespace + "." + mappedStatement.getId();
			configuration.getMappedStatementMap().put(key, mappedStatement);
		}
	}
}